package ming;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebFilter(filterName = "MyFilter",urlPatterns = "/main.jsp")
public class MyFilter implements Filter {
    public void init(FilterConfig fConfig) throws ServletException {
        // 过滤器对象在初始化时调用，可以配置一些初始化参数
    }
    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain chain) throws IOException, ServletException {
        response.setContentType("text/html;charset=utf-8");
        HttpServletRequest http_request = (HttpServletRequest)request;
        HttpSession session = http_request.getSession();
        User user = (User) session.getAttribute("user");
        if (user == null) {
            response.getWriter().print(
                    "您还没有登录，请<a href='/code_war_exploded/login'>登录</a>");
        }else {
//            http_request.getSession().setAttribute("user", user);
            request.getRequestDispatcher("/main.jsp").forward(request, response);
        }
    }
    public void destroy() {
        // 过滤器对象在销毁时自动调用，释放资源
    }
}

